import {view, BondViewGroup, handler} from "adajs";
import ContainerService from "./state.js";
import Router from "../../lib/router";
import User from "./../../lib/user";

@view({
    className: "container",
    template: "./template.html",
    style: "./style.scss",
    dataset: {
        service: ContainerService
    }
})
class Container extends BondViewGroup {
    oncreated() {
        User.check(this);
        let _router = this.router = new Router(this.context);
        this.getCurrentState().menu.forEach(item => {
            _router.bind(item.link === "/" ? "/" : item.link, (e) => {
                this.commit("flip", item.link);
                document.querySelector('title').innerHTML = item.title;
            });
        });
        User.setRequestTransformer(this, _router, this.context.request);
    }

    onready() {
        this.router.run();
    }

    @handler("gotoPage")
    gotoPage({data}) {
        this.router.open(data);
    }
}

export default Container;